package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.StageDefinePropertyListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.StageDefinePropertyOriginal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * editor: HuXiaoDan
 * time: 2016/7/4 10:42
 */

public class StageDefinePropertyListFragment extends BaseListFragment {
  /* 阶段ID*/
  public static final String EXTRA_SAGE_ID =
      "com.isunland.managesystem.ui.StageDefinePropertyListFragment.EXTRA_SAGE_ID";
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.StageDefinePropertyListFragment.EXTRA_CONTENT";
  private static final int DETAIL = 0;
  private StageDefinePropertyListAdapter mAdapter;
  private CurrentUser mUser;
  private CustomerDialog extContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    mUser = CurrentUser.newInstance(getActivity());
    extContent = (CustomerDialog) getArguments().getSerializable(EXTRA_CONTENT);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        //                if ((mUser.getJobNumber()).equals(extContent.getId()) ||(mUser.getJobNumber()).equals(extContent.getName())) {
        Intent intent = new Intent(getActivity(), StageDefinePropertyDetailActivity.class);
        intent.putExtra(StageDefinePropertyDetailFragment.EXTRA_VALUE, extContent);
        startActivityForResult(intent, DETAIL);
        //
        //                }else {
        //
        //                    ToastUtil.show(R.string.pm_regist);
        //                }

        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.ULR_STAGE_PROPERTY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", mUser.getMemberCode());
    params.put("stageId", extContent.getMemberCode());
    params.put("page", "1");
    params.put("rows", "1000");
    return params;
  }

  @Override public void resolveResponse(String json) {
    StageDefinePropertyOriginal original =
        new Gson().fromJson(json, StageDefinePropertyOriginal.class);
    List<StageDefinePropertyOriginal.StageDefinePropertyContent> rows = original.getRows();
    setAdapter(rows);
  }

  private void setAdapter(List<StageDefinePropertyOriginal.StageDefinePropertyContent> rows) {
    if (rows == null) {
      rows = new ArrayList<>();
    }
    if (mAdapter == null) {
      mAdapter = new StageDefinePropertyListAdapter(getActivity(), rows);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(rows);
    ((StageDefinePropertyListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    StageDefinePropertyOriginal.StageDefinePropertyContent content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), StageDefinePropertyDetailActivity.class);
    i.putExtra(StageDefinePropertyDetailFragment.EXTRA_CONTENT, content);
    i.putExtra(StageDefinePropertyDetailFragment.EXTRA_VALUE, extContent);
    startActivityForResult(i, DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DETAIL) {
      volleyPost();
    }
  }

  public static StageDefinePropertyListFragment newInstence(String stageId) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_SAGE_ID, stageId);
    StageDefinePropertyListFragment mfragment = new StageDefinePropertyListFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }

  public static StageDefinePropertyListFragment newInstence(CustomerDialog content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_CONTENT, content);
    StageDefinePropertyListFragment mfragment = new StageDefinePropertyListFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
